package com.cskaoyan.gateway.controller.shopping;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.IProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.AllProductRequest;
import com.mall.shopping.dto.AllProductResponse;
import com.mall.shopping.dto.ProductDetailRequest;
import com.mall.shopping.dto.ProductDetailResponse;
import com.mall.user.annotation.Anoymous;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("shopping")
public class ProductController {

    @Reference(timeout = 3000,check = false)
    IProductService productService;

    @ApiOperation("获取商品列表")
    @GetMapping("goods")
    @Anoymous
    public ResponseData AllProducts(
                     Integer page,
                     Integer size,
                     String sort,
                     Integer priceGt,
                     Integer priceLte
    ){
        AllProductRequest request = new AllProductRequest();
        request.setPage(page);
        request.setSize(size);
        request.setSort(sort);
        request.setPriceGt(priceGt);
        request.setPriceLte(priceLte);
        AllProductResponse response = productService.getAllProduct(request);
        HashMap<String, Object> map = new HashMap<>();
        map.put("data",response.getProductDtoList());
        map.put("total",response.getTotal());
        return new ResponseUtil<Map>().setData(map);
    }


    @Anoymous
    @GetMapping("product/{id}")
    public ResponseData showProduct(@PathVariable Long id){
        ProductDetailRequest detailRequest = new ProductDetailRequest();
        detailRequest.setId(id);

        ProductDetailResponse productDetail = productService.getProductDetail(detailRequest);
        if (productDetail.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(productDetail.getProductDetailDto());
        }
        return new ResponseUtil().setErrorMsg(productDetail.getMsg());
    }
}
